<?php

namespace app\service;

use app\controller\SystemUser;
use app\model\OrderPersonCertificateRegister;
use app\model\UserOrganization;

class OrderCertExamine
{
    public function spliceWhere($params = [])
    {
        $model = new OrderPersonCertificateRegister();
        $c_ids = $model->getIds();

        //证书id 用于证书匹配
        if (isset($params['certificate_id']) && $params['certificate_id']) {
            $list = $model
                ->where('certificate_id', $params['certificate_id'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书编号
        if (isset($params['certificate_code']) && $params['certificate_code']) {
            $list = $model
                ->where('certificate_code', 'like', '%' . $params['certificate_code'] . '%')
                ->field('id')
                ->select()
                ->toArray();

            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //创建时间
        if ((isset($params['create_start_date']) && $params['create_start_date']) && (isset($params['create_end_date']) && $params['create_end_date'])) {
            $start_time = $params['create_start_date'] . ' 00:00:00';
            $end_time = $params['create_end_date'] . ' 23:59:59';
            $list = $model->where('create_date', 'between', [$start_time, $end_time])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类别
        if (isset($params['certificate_leibie']) && $params['certificate_leibie']) {
            $list = $model
                ->where('certificate_leibie', $params['certificate_leibie'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类型
        if (isset($params['certificate_leixing']) && $params['certificate_leixing']) {

            $list = $model
                ->where('certificate_leixing', $params['certificate_leixing'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书等级
        if (isset($params['certificate_dengji']) && $params['certificate_dengji']) {

            $list = $model
                ->where('certificate_dengji', $params['certificate_dengji'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书专业
        if (isset($params['certificate_zhuanye']) && $params['certificate_zhuanye']) {
            $list = $model
                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //社保
        if (isset($params['social_security']) && $params['social_security']) {
            $list = $model
                ->where('social_security', $params['social_security'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //社保账户
        if (isset($params['social_security_acc_status']) && $params['social_security_acc_status']) {
            $list = $model
                ->where('social_security_acc_status', $params['social_security_acc_status'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //b证
        if (isset($params['certificate_b']) && $params['certificate_b']) {
            $list = $model
                ->where('certificate_b', $params['certificate_b'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //b证是否调入
        if (isset($params['b_doc_status']) && $params['b_doc_status']) {
            $list = $model
                ->where('b_doc_status', $params['b_doc_status'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //项目
        if (isset($params['project']) && $params['project']) {
            $list = $model
                ->where('project', $params['project'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //周期
        if (isset($params['cycle']) && $params['cycle']) {
//            $list = $model
//                ->where('cycle', $params['cycle'])
//                ->field('id')
//                ->select()
//                ->toArray();
            $list = $model
                    ->select ()
                    ->toArray ();
            //判断cycle是否包含接受值
            $listIdArr = [];
            foreach ($list as $listItem) {
                if (!empty($listItem['cycle']['id'])){
                    $str = explode (',',$listItem['cycle']['id']);
                    foreach ($str as $strItem) {
                        if ($strItem === $params['cycle']){
                            $listIdArr[] = ['id'=>$listItem['id']];
                        }
                    }
                }
            }
            $ids = array_column($listIdArr, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //是否中转
        if (isset($params['transfer']) && $params['transfer']) {
            $list = $model
                ->where('transfer', $params['transfer'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证时间
        if ((isset($params['acquisition_start_time_date']) && $params['acquisition_start_time_date']) && (isset($params['acquisition_end_time_date']) && $params['acquisition_end_time_date'])) {
            $start_time = $params['acquisition_start_time_date'] . ' 00:00:00';
            $end_time = $params['acquisition_end_time_date'] . ' 23:59:59';
            $list = $model->where('acquisition_date', 'between', [$start_time, $end_time])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //合同价格
        if ((isset($params['contract_start_price']) && ($params['contract_start_price'] || (int)($params['contract_start_price']) == 0)) && (isset($params['contract_end_price']) && $params['contract_end_price'])) {
            $list = $model->where('contract_price', 'between', [$params['contract_start_price'], $params['contract_end_price']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //实际价格
        if ((isset($params['catual_start_price']) && ($params['catual_start_price'] || (int)($params['catual_start_price']) == 0)) && (isset($params['catual_end_price']) && $params['catual_end_price'])) {
            $list = $model->where('catual_price', 'between', [$params['catual_start_price'], $params['catual_end_price']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }

        //人才应付价
        if ((isset($params['start_person_price']) && ($params['start_person_price'] || (int)($params['start_person_price']) == 0)) && (isset($params['end_person_price']) && $params['end_person_price'])) {
            $list = $model->where('person_price', 'between', [$params['start_person_price'], $params['end_person_price']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //备注
        if (isset($params['notes']) && $params['notes']){
            $list = $model
                ->where('notes', 'like', '%' . $params['notes'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //人才账号
        if (isset($params['person_account']) && $params['person_account']) {
            $list = $model
                ->where('person_account', 'like', '%' . $params['person_account'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //住建账户
        if(isset($params['zj_account']) && $params['zj_account']){
            $list = $model
                ->where('zj_account', 'like', '%' . $params['zj_account'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //电话
        if (isset($params['mobile']) && $params['mobile']) {
            $list = $model
                ->where('mobile',  $params['mobile'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证部门
        if (isset($params['get_org']) && $params['get_org']) {
            $department_ids = SystemUser::getDepartmentAllUser($params['get_org']);
            $list = $model
                ->where('get_org', 'in', $department_ids)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证人员
        if (isset($params['get_uid']) && $params['get_uid']) {
            $list = \app\model\User::where('nickname', 'like', '%'.$params['get_uid'].'%')
                ->field('id')
                ->select()
                ->toArray();
            $user_ids = array_column($list, 'id');
            $list = $model
                ->where('get_uid', 'in', $user_ids)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //状态
        if ((isset($params['status']) && $params['status']) || $params['status'] === 0 || $params['status'] === '0') {
            $list = $model
                ->where('status',  $params['status'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书特性
        if (isset($params['certificate_characteristic']) && $params['certificate_characteristic']) {
            $list = $model
                ->where('certificate_characteristic',  $params['certificate_characteristic'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //人才名称
        if (isset($params['person_name']) && $params['person_name']) {
            $list = $model
                ->where('person_name','like','%'.$params['person_name'].'%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //特价证书状态
        if (isset($params['certificate_characteristic_status']) && $params['certificate_characteristic_status']) {
            $list = $model
                ->where('certificate_characteristic_status',  $params['certificate_characteristic_status'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        return $c_ids;
    }

    public function handlePersonIds($enter_ids, $other_ids)
    {
        if (!$enter_ids || !$other_ids) {
            return [];
        }
        if ($enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif ($enter_ids && !$other_ids) {
            $enter_ids = [];
        } elseif (!$enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif (!$enter_ids && !$other_ids) {
            $enter_ids = [];
        }
        return $enter_ids;
    }

}